﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace BusinessLogic
{
    public class templetinfoLogic
    {

        public List<dynamic> getPageDate<T, TKey>(Expression<Func<T, dynamic>> select, Expression<Func<T, bool>> where, Expression<Func<T, TKey>> order, int pageIndex, int pageSize, out int Total)
                   where T : class
        {
            Model1Container db = new Model1Container();
            Total = db.Set<T>().Where(where).Count();
            var list = db.Set<T>().Where(where).OrderByDescending(order).Select(select).Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return list.ToList();
        }

        public templetinfo GetForm(int keyValue)
        {
            Model1Container db = new Model1Container();
            return db.templetinfo.Find(keyValue);
        }

        public int AddInfo(templetinfo model)
        {
            int result = 0;
            try
            {
                Model1Container db = new Model1Container();
                db.templetinfo.Add(model);
                db.SaveChanges();
                result = model.micro_id;
            }
            catch (Exception ex)
            {
                return 0;
            }
            return result;
        }

        public int UpdateInfo(templetinfo model)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var newModel = db.templetinfo.Where(c => c.micro_id == model.micro_id).First();
                    newModel.micro_id = model.micro_id;
                    newModel.micro_title = model.micro_title;
                    newModel.remark = model.remark;
                    newModel.order_by = model.order_by;
                    newModel.share_describe = model.share_describe;
                    newModel.share_path = model.share_path;
                    newModel.share_title = model.share_title;
                    newModel.background_bottom = model.background_bottom;
                    newModel.background_height = model.background_height;
                    newModel.background_width = model.background_width;
                    newModel.background_path = model.background_path;
                    newModel.brand_id = model.brand_id;
                    newModel.collect_number = model.collect_number;
                    newModel.copy_text = model.copy_text;
                    newModel.cover_path = model.cover_path;
                    newModel.create_time = model.create_time;
                    newModel.false_click = model.false_click;
                    //newModel.is_delete = model.is_delete;
                    newModel.is_dynamic = model.is_dynamic;
                    newModel.is_enable = model.is_enable;
                    newModel.is_top = model.is_top;
                    newModel.ispay = model.ispay;
                    newModel.isvip = model.isvip;
                    newModel.tipshow = model.tipshow;
                    newModel.true_click = model.true_click;
                    newModel.update_time = model.update_time;
                    newModel.category_id = model.category_id;
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }

        public int DelInfo(int id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.templetinfo.First(p => p.micro_id == id);
                    model.is_delete = 1;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }

        public int Show(int id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.templetinfo.First(p => p.micro_id == id);
                    model.is_enable = 1;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }
        public int UnShow(int id)
        {
            int result = 0;
            try
            {
                using (Model1Container db = new Model1Container())
                {
                    var model = db.templetinfo.First(p => p.micro_id == id);
                    model.is_enable = 0;
                    db.SaveChanges();
                    result = db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                return 0;
            }

            return result;
        }

        //public int Open(int id)
        //{
        //    int result = 0;
        //    try
        //    {
        //        using (Model1Container db = new Model1Container())
        //        {
        //            var model = db.templetinfo.First(p => p.micro_id == id);
        //            model.IsOpen = 1;
        //            db.SaveChanges();
        //            result = db.SaveChanges();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        return 0;
        //    }

        //    return result;
        //}
        //public int UnOpen(int id)
        //{
        //    int result = 0;
        //    try
        //    {
        //        using (Model1Container db = new Model1Container())
        //        {
        //            var model = db.Shop.First(p => p.ID == id);
        //            model.IsOpen = 0;
        //            db.SaveChanges();
        //            result = db.SaveChanges();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        return 0;
        //    }

        //    return result;
        //}

        //public int Pic_Shop(string ids, int ShopID)
        //{
        //    int id = 0;
        //    if (!string.IsNullOrEmpty(ids))
        //    {
        //        using (Model1Container db = new Model1Container())
        //        {
        //            db.T_Pic.RemoveRange(db.T_Pic.Where(c => c.SouceID == ShopID && c.Type == 1));
        //            db.SaveChanges();
        //        }
        //        using (Model1Container db = new Model1Container())
        //        {

        //            var temarry = ids.Split(',');
        //            for (int i = 0; i < temarry.Length; i++)
        //            {
        //                if (!string.IsNullOrEmpty(temarry[i]))
        //                {
        //                    T_Pic model = new T_Pic();
        //                    model.SouceID = ShopID;
        //                    model.Url = temarry[i];
        //                    model.Type = 1;
        //                    model.Details = "";

        //                    db.T_Pic.Add(model);
        //                }
        //            }
        //            db.SaveChanges();
        //        }
        //    }
        //    return id;
        //}

        //public int Pic_TempletInfo(string ids, int TempletID)
        //{
        //    int id = 0;
        //    if (!string.IsNullOrEmpty(ids))
        //    {
        //        using (Model1Container db = new Model1Container())
        //        {
        //            db.T_Pic.RemoveRange(db.T_Pic.Where(c => c.SouceID == TempletID && c.Type == 1));
        //            db.SaveChanges();
        //        }
        //        using (Model1Container db = new Model1Container())
        //        {

        //            var temarry = ids.Split(',');
        //            for (int i = 0; i < temarry.Length; i++)
        //            {
        //                if (!string.IsNullOrEmpty(temarry[i]))
        //                {
        //                    T_Pic model = new T_Pic();
        //                    model.SouceID = ShopID;
        //                    model.Url = temarry[i];
        //                    model.Type = 1;
        //                    model.Details = "";

        //                    db.T_Pic.Add(model);
        //                }
        //            }
        //            db.SaveChanges();
        //        }
        //    }
        //    return id;
        //}
    }
}
